/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.starmeep.classes;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.exec.environment.EnvironmentUtils;


/**
 *
 * @author marc
 */
public class StarCommand {
    public static String runCommand(String cmd) {
//        String message="";
//        String[] cmdEnv = {"/bin/sh", "-c", cmd};
//        Runtime run = Runtime.getRuntime();
//        System.out.println(cmd);
//	Process pr = run.exec(cmdEnv);
//	pr.waitFor();
//	BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
//	String line = "";
//	while ((line=buf.readLine())!=null) {
//		//System.out.println(line);
//            message+=line;
//            message+="\n";
//	}
//        return message;
        ByteArrayOutputStream stdout = new ByteArrayOutputStream();
        PumpStreamHandler psh = new PumpStreamHandler(stdout);
        CommandLine cl = CommandLine.parse(cmd);
        DefaultExecutor exec = new DefaultExecutor();

        exec.setStreamHandler(psh);
        try {






            Map env=EnvironmentUtils.getProcEnvironment();
            EnvironmentUtils.addVariableToEnvironment(env, "TERM=xterm");
            EnvironmentUtils.addVariableToEnvironment(env, "CATALINA_HOME=/usr/local/apache-tomcat-6.0.26");
            EnvironmentUtils.addVariableToEnvironment(env, "XDG_SESSION_COOKIE=0048e5ae38ea44fe75727d3f4b8e9d56-1280803133.102286-1649262975");
            EnvironmentUtils.addVariableToEnvironment(env, "SSH_CLIENT=190.154.97.174 52722 22");
            EnvironmentUtils.addVariableToEnvironment(env, "SSH_TTY=/dev/pts/0");
            EnvironmentUtils.addVariableToEnvironment(env, "USER=root");
            EnvironmentUtils.addVariableToEnvironment(env, "MAIL=/var/mail/root");
            EnvironmentUtils.addVariableToEnvironment(env, "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games");
            EnvironmentUtils.addVariableToEnvironment(env, "PWD=/root");
            EnvironmentUtils.addVariableToEnvironment(env, "JAVA_HOME=/usr/lib/jvm/java-6-sun");
            EnvironmentUtils.addVariableToEnvironment(env, "LANG=en_US.UTF-8");
            EnvironmentUtils.addVariableToEnvironment(env, "SHLVL=1");
            EnvironmentUtils.addVariableToEnvironment(env, "HOME=/root");
            EnvironmentUtils.addVariableToEnvironment(env, "LOGNAME=root");
            EnvironmentUtils.addVariableToEnvironment(env, "SSH_CONNECTION=190.154.97.174 52722 10.202.197.3 22");
            EnvironmentUtils.addVariableToEnvironment(env, "LESSOPEN=| /usr/bin/lesspipe %s");
            EnvironmentUtils.addVariableToEnvironment(env, "LESSCLOSE=/usr/bin/lesspipe %s %s");
            EnvironmentUtils.addVariableToEnvironment(env, "TERM=xterm");
            EnvironmentUtils.addVariableToEnvironment(env, "OLDPWD=/root");
            EnvironmentUtils.addVariableToEnvironment(env, "_=/usr/bin/env");
            exec.execute(cl,env);
        } catch (ExecuteException ex) {
            Logger.getLogger(StarCommand.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(StarCommand.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(stdout.toString());
        return stdout.toString();

   }
}
